package hn.sigit.logic.ladm;

import hn.sigit.util.PropertyResponsibilitiesComparable;
import hn.sigit.util.ResponsibilitiesComparable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResponsibilitiesUtil {
	public static boolean propertyResponsibilitiesEqual(PropertyResponsibilitiesComparable prc1, PropertyResponsibilitiesComparable prc2) {
		if (prc1.propertyResponsibilitiesHash() == prc2.propertyResponsibilitiesHash()) {
			Map<Integer, ResponsibilitiesComparable> responsibilitiesMap = new HashMap<Integer, ResponsibilitiesComparable>();
			for (ResponsibilitiesComparable rc : prc1.getResponsibilitiesComparableList())
				responsibilitiesMap.put(rc.responsibilitiesHash(), rc);
			
			Map<Integer, ResponsibilitiesComparable> otherResponsibilitiesMap = new HashMap<Integer, ResponsibilitiesComparable>();
			for (ResponsibilitiesComparable rc : prc2.getResponsibilitiesComparableList())
				otherResponsibilitiesMap.put(rc.responsibilitiesHash(), rc);
			
			if ( responsibilitiesMap.keySet().equals( otherResponsibilitiesMap.keySet() ) )
				for (ResponsibilitiesComparable rc : responsibilitiesMap.values())
					if (!rc.responsibilitiesEquals(otherResponsibilitiesMap.get(rc.responsibilitiesHash())))
						return false;
			
			return true;
		}
		return false;
	}
	
	public static boolean responsibilitiesEqual(ResponsibilitiesComparable rc1, ResponsibilitiesComparable rc2) {
		if (rc1.responsibilitiesHash() == rc2.responsibilitiesHash()) {
			return rc1.getShare().hashCode() == rc2.getShare().hashCode()
					&& rc1.getExtPID() == rc2.getExtPID()
					&& rc1.getType().equals(rc2.getType());
		}
		return false;
	}
	
	public static boolean propertyHasResponsibility(PropertyResponsibilitiesComparable theProperty, ResponsibilitiesComparable responsibility) {
		List<ResponsibilitiesComparable> responsibilityList = theProperty.getResponsibilitiesComparableList();
		for (ResponsibilitiesComparable rc : responsibilityList) {
			if (responsibilitiesEqual(rc, responsibility))
				return true;
		}
		
		return false;
	}

}
